<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/11/1
 * Time: 15:20
 */

namespace app\adminapiv3\controller\platform_manage;

use app\adminapiv3\controller\BaseController;
use app\adminapiv3\model\ServerHostGroupModuleModel;
use app\adminapiv3\service;
use app\common\exception\CustomException;
use think\Request;

//授权管理
class ServerAuthController extends BaseController
{
    /**
     * @name 授权列表
     * @author wx
     * @date 2019/11/6 16:54
     */
    public function list(Request $request)
    {
        //当前资产组
        $groupId = $this->getLoginHostGroupId();
        //搜索参数
        $params = $request->only([
            'product_name','product_m_version','number','number','etime','stime'
        ],'get');

        $server = new \app\adminapiv3\service\platform_manage\ServerAuthService();

        $list = $server->list($params, $groupId);

        return $this->jsonOutPut($list);
    }

    /**
     * @name 在线注册
     * @author wx
     * @date 2019/11/6 16:55
     */
    public function online(Request $request)
    {
        //参数
        $params = $request->only('key','post');
        //数据验证
        $this->validate($params,'ServerAuth.online');
        $authServer = new \app\adminapiv3\service\platform_manage\ServerAuthService();
        $authServer->online($params['key']);
        return $this->jsonOutPut();
    }

    /**
     * @name 离线注册
     * @author wx
     * @date 2019/11/6 16:56
     */
    public function offline(Request $request)
    {
        $file = $request->file('file');
        if (!$file) {
            throw new  CustomException('请上传授权文件',122);
        }

        $authServer = new \app\adminapiv3\service\platform_manage\ServerAuthService();
        $authServer->offline($file);

        return $this->jsonOutPut();
    }

    /**
     * @name: 登录前平台授权信息
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-20 17:43:49
     */
    public function platformAuthInfo ()
    {
        $ServerAuth = new service\platform_manage\ServerAuthService();
        $data = $ServerAuth->platformAuthInfo();

        return $this->jsonOutPut($data);
    }

    /**
     * 资产组授权信息查询接口(登录后)
    */
    public function userGroupAuthInfo(){
        //当前资产组
        $groupId = $this->getLoginHostGroupId();
        $userModuleModel = new ServerHostGroupModuleModel();
        $info = $userModuleModel->getAuthInfo($groupId);
        return $this->jsonOutPut($info);
    }

    /**
     * @name 获取
     * @author wx
     * @date 2020/12/15 11:27
     */
    public function getHostFile(Request $request)
    {
        $domain = $request->domain();

        $data['ip'] = $_SERVER['SERVER_ADDR'];
        $data['port'] = $_SERVER['SERVER_PORT'];
        $data['path'] = 'static/host/hostkey.key';
        $data['url'] = $domain.'/' .$data['path'];

        $server = new service\platform_manage\ServerAuthService();
        $server->createHostFile();

        return $this->jsonOutPut($data);
    }

}